package org.biojava.services.das.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.biojava.dasobert.dasregistry.DasSource;
import org.biojava.services.das.registry.DasRegistrySql;
import org.biojava.services.das2.Das1SourceDbProvider;
import org.biojava.services.das2.DasSourceProvider;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dasregistry.utils.RegistryCache;
import dasregistry.utils.SingletonRegistryCache;

import uk.ac.sanger.www.WTSISearchService.GetResultsFromSanger;

/**
 * Servlet implementation class SearchSangerServlet
 */
public class SearchSangerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	GetResultsFromSanger sanger=new GetResultsFromSanger();
	DasSourceProvider das1provider;
	SingletonRegistryCache cache;
  
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		cache =(SingletonRegistryCache)ctx.getBean("registryCache");

	}

	public void destroy() {
		das1provider=null;
	}
	
	/**
     * @see HttpServlet#HttpServlet()
     */
    public SearchSangerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String queryString = request.getQueryString();
		System.out.println("queryStirng=" + queryString);
		String cmdPath = request.getPathInfo();
		if (cmdPath == null) {
			cmdPath = "";
		}
		System.out.println("commandPath " + cmdPath);

		StringTokenizer toke = new StringTokenizer(cmdPath, "/");
		

		
		String command = toke.nextToken();
		
		
		List ids=null;
		System.out.println("request to search");
		String query=request.getParameter("query");
		if(command.equalsIgnoreCase("coordinates")){
		ids=sanger.getCoordinatesForKeyWords(query);
		}
		if(command.equalsIgnoreCase("sources")){
			List<DasSource> sourcesToWrite=new ArrayList<DasSource>();
			ids=sanger.getSourcesForKeywordsSearch(query);
			List<DasSource> sources = this.getDas1Sources(request);
			for(DasSource source:sources){
				String sourceId=source.getId();
				System.out.println(sourceId);
				if(ids.contains(sourceId))sourcesToWrite.add(source);
			}
			System.out.println("sourcesToWrite size="+sourcesToWrite.size());
			
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	
	public List<DasSource> getDas1Sources(HttpServletRequest request) {
		BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		DasRegistrySql registry=(DasRegistrySql)ctx.getBean("registryBean");
		Date d = registry.getlastModified();
		Long lastModified=d.getTime();
		List<DasSource> sources = cache.getDas1Sources();
		return sources;
	}
}
